package org.primefaces.examples.moviecollector.dao;
import static org.junit.Assert.*;

import java.util.List;


import org.junit.Test;
import org.junit.runner.RunWith;
import org.primefaces.examples.moviecollector.domain.SitiosTuristicos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;


@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@TransactionConfiguration(transactionManager = "txManager")
@ContextConfiguration(locations = { "/applicationContext.xml" })


public class SitiosTuristicosDAOWithJPATest {

	private SitiosTuristicosDAO sitioTDAO;
	
	@Autowired
	public void setRepository(SitiosTuristicosDAO sitioTDAO){
		this.sitioTDAO=sitioTDAO;
	}
	
	@Test
	public void guardarSitio(){
		SitiosTuristicos sitioT = new SitiosTuristicos();
		sitioT.setNombre("Universidad Santiago de Cali");
		
		sitioTDAO.persist(sitioT);
		assertNotNull(sitioT.getId());
	}
	
	@Test
	public void buscarPorNombre(){
		SitiosTuristicos sitioT = new SitiosTuristicos();
		sitioT.setNombre("Tajamares");
		sitioT.setClasificacion("Restaurante");
		
		sitioTDAO.persist(sitioT);
		assertNotNull(sitioT.getId());
		
		List<SitiosTuristicos> resultado = sitioTDAO.findByNombre("Restaurante Tajamares");
		assertEquals(1, resultado.size());
		assertEquals("Restaurante Tajamares", resultado.get(0).getNombre());
		
	}
	
	@Test
	public void eliminar(){
	
		SitiosTuristicos sitioT = new SitiosTuristicos();
		sitioT.setNombre("Gobernacion");
		
		sitioTDAO.persist(sitioT);
		
		sitioTDAO.delete(sitioT);
		
		
		assertFalse((!sitioT.getNombre().equals("Gobernacion")));
		
		
	}
}

